package com.yanqu.road.server.manager.activity.seacraftsouthsea;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.seacraft.*;
import com.yanqu.road.entity.activity.seacraft.config.SeacraftSouthSeaActivityConfig;
import com.yanqu.road.logic.bussiness.activity.SeacraftSouthSeaBusiness;
import com.yanqu.road.server.manager.activity.ActivityMgr;
import com.yanqu.road.server.manger.SeacraftSouthSeaCommonMgr;
import com.yanqu.road.utils.string.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SeacraftSouthSeaActivityGroup {
    private final Object lock = new Object();
    private static Logger logger = LogManager.getLogger(SeacraftSouthSeaActivityGroup.class.getName());
    private int activityId;
    private Map<Integer, SeacraftSouthSeaActivity> groupMap = new ConcurrentHashMap<>();
    /**
     * 参与的联盟信息
     */
    private Map<String, SeacraftSouthSeaUnionInfo> unionInfoMap = new ConcurrentHashMap<>();

    /**
     * 玩家数据
     */
    private Map<Long, SeacraftSouthSeaCrossUserInfo> userInfoMap = new ConcurrentHashMap<>();

    public SeacraftSouthSeaActivityGroup(int activityId) {
        this.activityId = activityId;
        loadDbConfig();
        loadDbData();
    }

    public void loadDbConfig() {
        // 加载配置
        SeacraftSouthSeaConfigMgr.reloadConfig(activityId);
    }

    public void loadDbData(){
        Map<String, SeacraftSouthSeaCrossUnionData> unionDataMap = SeacraftSouthSeaBusiness.getSeacraftSouthSeaCrossUnionDataMap(activityId);
        unionDataMap.values().forEach(e -> {
            unionInfoMap.put(e.getUnionId(), new SeacraftSouthSeaUnionInfo(e));
        });
        Map<String, SeacraftSouthSeaCrossUnionShipData> shipDataMap = SeacraftSouthSeaBusiness.getSeacraftSouthSeaCrossUnionShipDataMap(activityId);
        shipDataMap.values().forEach(e -> {
            SeacraftSouthSeaUnionInfo unionInfo = unionInfoMap.get(e.getUnionId());
            if (unionInfo == null) {
                logger.error("海上争霸 南海丝路 有SeacraftSouthSeaCrossUnionShipData数据 却没有 联盟数据 activityId:{},unionId:{}", e.getActivityId(), e.getUnionId());
            } else {
                unionInfo.setShipData(e);
            }
        });

        Map<Long, SeacraftSouthSeaCrossUserData> userDataMap = SeacraftSouthSeaBusiness.getSeacraftSouthSeaCrossUserDataMap(activityId);
        userDataMap.values().forEach(e -> {
            SeacraftSouthSeaCrossUserInfo userInfo = new SeacraftSouthSeaCrossUserInfo(e);
            userInfoMap.put(e.getUserId(), userInfo);
            if (!StringUtils.isNullOrEmpty(e.getUnionId())) {
                SeacraftSouthSeaUnionInfo unionInfo = unionInfoMap.get(e.getUnionId());
                if (unionInfo == null) {
                    logger.info("海上争霸 南海丝路 有SeacraftSouthSeaCrossUserData数据 却没有 联盟数据 activityId:{},unionId:{},userId:{}", e.getActivityId(), e.getUnionId(), e.getUserId());
                } else {
                    unionInfo.getMemberMap().put(e.getUserId(), userInfo);
                }
            }
        });
        Map<Long, SeacraftSouthSeaCrossChallengeData> challengeMap = SeacraftSouthSeaBusiness.getSeacraftSouthSeaCrossChallengeDataMap(activityId);
        challengeMap.values().forEach(e -> {
            SeacraftSouthSeaCrossUserInfo userInfo = userInfoMap.get(e.getUserId());
            if (userInfo == null) {
                logger.info("海上争霸 南海丝路 有SeacraftSouthSeaCrossChallengeData数据 却没有 玩家数据 activityId:{},userId:{}", e.getActivityId(), e.getUserId());
            } else {
                userInfo.setChallengeData(e);
            }
        });
        int curRound = getCurRound();
        long time = 0;
        if (curRound > 0) {
            time = SeacraftSouthSeaMgr.getRoundStartTime(activityId, curRound);
        }
        List<SeacraftSouthSeaBattleDetailData> battleDetailList = SeacraftSouthSeaBusiness.getSeacraftSouthSeaBattleDetailDataList(activityId, time);
        battleDetailList.forEach(e -> {
            SeacraftSouthSeaUnionInfo unionInfo = unionInfoMap.get(e.getUnionId());
            if (unionInfo == null) {
                logger.error("海上争霸 南海丝路 有SeacraftSouthSeaBattleDetailData数据 却没有 联盟数据 activityId:{},unionId:{}", e.getActivityId(), e.getUnionId());
            } else {
                if (e.getMasterType() == 0) {
                    unionInfo.getAttackBattleMap().put(e.getTargetUnionId(), e);
                } else if (e.getMasterType() == 1) {
                    unionInfo.getDefendBattleMap().put(e.getTargetUnionId(), e);
                }
            }
        });
        List<SeacraftSouthSeaCrossRecordData> list = SeacraftSouthSeaBusiness.getSeacraftSouthSeaCrossRecordDataList(activityId, time);
        list.forEach(e -> {
            SeacraftSouthSeaUnionInfo unionInfo = unionInfoMap.get(e.getUnionId());
            if (unionInfo == null) {
                logger.error("海上争霸 南海丝路 有SeacraftSouthSeaCrossRecordData数据 却没有 联盟数据 activityId:{},unionId:{}", e.getActivityId(), e.getUnionId());
            } else {
                unionInfo.addRecord(e);
            }
        });
    }

    public void save() {
        for (SeacraftSouthSeaActivity southSeaActivity : groupMap.values()) {
            southSeaActivity.save();
        }
    }

    public int getCurRound() {
        SeacraftSouthSeaActivityConfig activityConfig = SeacraftSouthSeaConfigMgr.getConfigMap().get(activityId);
        if (activityConfig == null) {
            return 0;
        }
        ActivityInfo activityInfo = ActivityMgr.getActivityInfo(activityId);
        if (activityInfo == null) {
            return 0;
        }
        return SeacraftSouthSeaCommonMgr.getCurRound(activityInfo, activityConfig);
    }

    public SeacraftSouthSeaActivity getActivity(int groupId, boolean newIfNull) {
        if (!groupMap.containsKey(groupId) && newIfNull) {
            synchronized (lock) {
                if (!groupMap.containsKey(groupId)) {
                    SeacraftSouthSeaActivity activity = new SeacraftSouthSeaActivity(activityId, groupId);
                    activity.initActivityInfo(unionInfoMap, userInfoMap);
                    groupMap.put(groupId, activity);
                }
            }
        }

        return groupMap.get(groupId);
    }

    public int getActivityId() {
        return activityId;
    }

    public List<SeacraftSouthSeaActivity> getActivityList() {
        return new ArrayList<>(groupMap.values());
    }
}
